package com.btex.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.btex.app.adapter.SearchAdapter;
import com.btex.app.entity.MakeListInfo;
import com.btex.app.entity.SearchInfo;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SeaechActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.activity_search_back_imag)
    ImageView mActivitySearchBackImag;
    @BindView(R.id.activity_search_ed)
    EditText mActivitySearchEd;
    @BindView(R.id.activity_search)
    RecyclerView mActivitySearch;
    private MakeListInfo mMakeListInfo;

    private ArrayList<SearchInfo> mSearchInfos;
    private SearchAdapter mSearchAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seaech);
        ButterKnife.bind(this);
        initRecyClerview();
        getMakeListInfo();

        initEditListener();
        mActivitySearchEd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {

                InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(mActivitySearchEd.getWindowToken(), 0);

                return true;

            }
        });
    }

    private void initRecyClerview() {


        mActivitySearch.setLayoutManager(new LinearLayoutManager(this));
        mSearchAdapter = new SearchAdapter(null);
        mActivitySearch.setAdapter(mSearchAdapter);
        mSearchAdapter.setOnItemClickListener(this);
    }

    private void initEditListener() {
        mActivitySearchEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String trim = editable.toString().trim().toUpperCase();
//                Log.i("kwjefhkwuhfwhf", trim);


                if (trim.equals("")) {
                    mSearchAdapter.setNewData(null);
                } else {
                    searchMe(trim);
                }


            }
        });
    }

    private void searchMe(String trim) {

//        RequestParams params = new RequestParams();
//
//        new HttpUtils().send(HttpRequest.HttpMethod.GET, Config.SEARCH_MARKETS, params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//
//                String result = responseInfo.result;
//                Log.i("lwefjwjefoiwe", result);
//
//
//            }
//
//            @Override
//            public void onFailure(HttpException error, String msg) {
//
//
//            }
//        });


        if (mMakeListInfo != null) {

            List<MakeListInfo.DataBean.BTCBean> btc = mMakeListInfo.getData().getBTC();
            List<MakeListInfo.DataBean.DOGEBean> doge = mMakeListInfo.getData().getDOGE();
            List<MakeListInfo.DataBean.ETHBean> eth = mMakeListInfo.getData().getETH();
            List<MakeListInfo.DataBean.USDTBean> usdt = mMakeListInfo.getData().getUSDT();


            mSearchInfos = new ArrayList<>();

            mSearchInfos.clear();


            if (btc != null) {
                for (int i = 0; i < btc.size(); i++) {

                    SearchInfo mSearchInfo = new SearchInfo();
                    if (btc.get(i).getType().contains(trim)) {
                        mSearchInfo.setType(btc.get(i).getType());
                        mSearchInfo.setDanwei(btc.get(i).getDanwei());
                        mSearchInfos.add(mSearchInfo);

                    }


                }
            }

            if (doge != null) {

                for (int i = 0; i < doge.size(); i++) {

                    SearchInfo mSearchInfo = new SearchInfo();
                    if (doge.get(i).getType().contains(trim)) {
                        mSearchInfo.setType(doge.get(i).getType());
                        mSearchInfo.setDanwei(doge.get(i).getDanwei());
                        mSearchInfos.add(mSearchInfo);

                    }


                }
            }


            if (eth != null) {
                for (int i = 0; i < eth.size(); i++) {
                    SearchInfo mSearchInfo = new SearchInfo();
                    if (eth.get(i).getType().contains(trim)) {
                        mSearchInfo.setType(eth.get(i).getType());
                        mSearchInfo.setDanwei(eth.get(i).getDanwei());
                        mSearchInfos.add(mSearchInfo);

                    }


                }
            }


            if (usdt != null) {
                for (int i = 0; i < usdt.size(); i++) {

                    SearchInfo mSearchInfo = new SearchInfo();
                    if (usdt.get(i).getType().contains(trim)) {
                        mSearchInfo.setType(usdt.get(i).getType());
                        mSearchInfo.setDanwei(usdt.get(i).getDanwei());

                        mSearchInfos.add(mSearchInfo);

                    }


                }
            }


            if (mSearchInfos.size() > 0) {
//                int size = mSearchInfos.size();

                mSearchAdapter.setNewData(mSearchInfos);
                mActivitySearch.setAdapter(mSearchAdapter);

            } else {
                mSearchAdapter.setNewData(null);
            }


        }


    }

    @Override
    public void setData() {

    }

    @Override
    public void onClick(View view) {

    }

    @OnClick({R.id.activity_search_back_imag, R.id.activity_search_ed})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_search_back_imag:


                InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
                //隐藏软键盘
                imm.hideSoftInputFromWindow(mActivitySearchEd.getWindowToken(), 0);
                finish();
                break;
            case R.id.activity_search_ed:
                break;
        }
    }


    public void getMakeListInfo() {

        SharedPreferencesUtil sp = SharedPreferencesUtil.getSharedPreferences(this);
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(this, sp);
        int num = (int) ((Math.random() * 9 + 1) * 100000);
        RequestParams params = new RequestParams();
        params.addHeader("myclient", "android");
        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.GET_MARKET_LISTS + "?rnd=" + String.valueOf(num),params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                String result = responseInfo.result;
//                Log.i("nbvcnxmfghjdk===BTC", result);


                Gson gson = new Gson();
                mMakeListInfo = gson.fromJson(result, MakeListInfo.class);


            }

            @Override
            public void onFailure(HttpException error, String msg) {


            }
        });

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {


        String pair = "";
        SearchInfo searchInfo = (SearchInfo) adapter.getData().get(position);
        String type = searchInfo.getType();
        String danwei = searchInfo.getDanwei();
        pair = type + "_" + danwei;
        Intent intent = new Intent(this, TradeActivity.class);
        intent.putExtra("pair", pair);
        startActivity(intent);
        finish();


    }
}
